package com.yun.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.reggie.common.CustomException;
import com.yun.reggie.entity.Category;
import com.yun.reggie.entity.Dish;
import com.yun.reggie.entity.Setmeal;
import com.yun.reggie.mapper.CategoryMapper;
import com.yun.reggie.service.CategoryService;
import com.yun.reggie.service.DishService;
import com.yun.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiao
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 删除分类之前，先判断是否有菜品/套餐关联,如果有抛出业务异常
     * @param ids
     * @return
     */
    @Override
    public void delete(Long ids) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(dishWrapper);
        if (count1 > 0){
            //抛出业务异常
            throw new CustomException("当前分类关联了菜品，不能删除！");
        }

        LambdaQueryWrapper<Setmeal> setMealWrapper = new LambdaQueryWrapper<>();
        setMealWrapper.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(setMealWrapper);
        if (count2 > 0){
            //抛出业务异常
            throw new CustomException("当前分类关联了套餐，不能删除！");
        }
        //正常删除
        super.removeById(ids);
    }
}
